<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title data-i18n="resources.title_popDensityRange"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }
        /*图例 style*/
        .legend {
            position: absolute;
            right: 10px;
            top: 350px;
            width: 250px;
            text-align: center;
            border: 2px solid #D6E3F1;
            background: #FFF;
            z-index: 999999;
            display: none;
        }

        .legendTitle {
            background: #1E90FF;
        }

        .legendContent {
            padding-left: 15px;
            padding-right: 15px;
            height: 160px;
            display: block;
            overflow-y: auto;
        }

        .legendItemHeader {
            top: 5px;
            width: 100px;
            height: 18px;
            text-align: center;
        }

        .legendItemValue {
            top: 5px;
            width: 120px;
            text-align: center;
            height: 18px;
        }

        /*信息框 style*/
        #infoBox {
            border: 2px solid #D6E3F1;
            position: absolute;
            right: 10px;
            top: 250px;
            width: 250px;
            z-index: 999999;
            display: none;
        }

        .editPane {
            position: absolute;
            right: 60px;
            top: 50px;
            text-align: center;
            background: #FFF;
            z-index: 1000;
        }
    </style>
</head>
<body>
<div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_graduatedSymbol"></h5>
    </div>
    <div class='panel-body' id='params'>
        <p></p>
        <div align='right' class='button-group'>
            <input type='button' id='btn1' class='btn btn-primary' data-i18n="[value]resources.btn_addThemeLayer"
                   onclick="addThemeLayer()"/>
            <input type='button' id='btn2' class='btn btn-primary' data-i18n="[value]resources.btn_updateData"
                   onclick="updateData()"/>
            <input type='button' id='btn3' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear"
                   onclick="clearLayer()"/>
        </div>
    </div>
</div>
<div>
    <div id="map" ></div>
    <div id="mapLegend" class="legend">
        <div class="legendTitle">
            <span data-i18n="resources.text_legend"></span>
        </div>
        <div class="legendContent">
            <table>
                <tr>
                    <td class="legendItemHeader" data-i18n="resources.text_populationDensity"></td>
                    <td class="legendItemValue" data-i18n="resources.text_color"></td>
                </tr>
                <tr>
                    <td class="legendItemHeader">0 - 0.02</td>
                    <td class="legendItemValue" style="background: #FDE2CA"></td>
                </tr>
                <tr>
                    <td class="legendItemHeader">0.02 - 0.04</td>
                    <td class="legendItemValue" style="background: #FACE9C"></td>
                </tr>
                <tr>
                    <td class="legendItemHeader">0.04 - 0.06</td>
                    <td class="legendItemValue" style="background: #F09C42"></td>
                </tr>
                <tr>
                    <td class="legendItemHeader">0.06 - 0.1</td>
                    <td class="legendItemValue" style="background: #D0770B"></td>
                </tr>
                <tr>
                    <td class="legendItemHeader">0.1 - 0.2</td>
                    <td class="legendItemValue" style="background: #945305"></td>
                </tr>
            </table>
        </div>
    </div>

    <div id="infoBox">
        <div data-i18n="resources.text_attributeTable" style="text-align: center;background: #1E90FF"> </div>
        <div id="infoContent" style="overflow-y: auto; padding: 5px; background-color: #FFFFFF">
        </div>
    </div>
</div>

<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script type="text/javascript">
    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090",
        url1 = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
        url2 = host + "/iserver/services/data-jingjin/rest/data";
    var map, layer, themeLayer;
    // 检测是否支持 Canvas
    if (!document.createElement('canvas').getContext) {
        widgets.alert.showAlert(resources.msg_supportCanvas, false);
    }

    map = new SuperMap.Map("map", {
        controls: [
            new SuperMap.Control.LayerSwitcher(),
            new SuperMap.Control.ScaleLine(),
            new SuperMap.Control.Zoom(),
            new SuperMap.Control.Navigation({
                dragPanOptions: {
                    enableKinetic: true
                }
            })]
    });
    layer = new SuperMap.Layer.TiledDynamicRESTLayer("Jingjin", url1, {
        transparent: true,
        cacheEnabled: true
    }, {maxResolution: "auto"});
    layer.events.on({"layerInitialized": addLayer});

    // 定义 Range 分段专题图层
    themeLayer = new SuperMap.Layer.Range("ThemeLayer");
    themeLayer.setOpacity(0.8);

    // 图层基础样式
    themeLayer.style = {
        shadowBlur: 16,
        shadowColor: "#000000",
        fillColor: "#FFFFFF"
    };

    // 开启 hover 高亮效果
    themeLayer.isHoverAble = true;
    // hover高亮样式
    themeLayer.highlightStyle = {
        stroke: true,
        strokeWidth: 4,
        strokeColor: 'blue',
        fillColor: "#00EEEE",
        //shadowBlur: 6,
        //shadowColor: "#000000",
        //shadowOffsetX: 6,
        //shadowOffsetY: 6,
        fillOpacity: 0.8
    };

    // 用于范围分段的属性字段名称
    themeLayer.themeField = "POP_DENSITY99";
    // 风格数组，设定分段范围对应的样式
    themeLayer.styleGroups = [
        {
            start: 0,
            end: 0.02,
            style: {
                color: '#FDE2CA'
            }
        },
        {
            start: 0.02,
            end: 0.04,
            style: {
                color: '#FACE9C'
            }
        },
        {
            start: 0.04,
            end: 0.06,
            style: {
                color: '#F09C42'
            }
        },
        {
            start: 0.06,
            end: 0.1,
            style: {
                color: '#D0770B'
            }
        },
        {
            start: 0.1,
            end: 0.2,
            style: {
                color: '#945305'
            }
        }
    ]

    // 注册 mousemove 事件
    themeLayer.on("mousemove", evn);

    function addLayer() {
        map.addLayers([layer, themeLayer]);
        map.setCenter(new SuperMap.LonLat(117.2, 40.11), 0);
    }

    //获取 feature 数据, 专题图的数据必须是 SuperMap.Feature.Vector
    function addThemeLayer() {
        clearLayer();

        var getFeatureParam, getFeatureBySQLService, getFeatureBySQLParams;

        getFeatureParam = new SuperMap.REST.FilterParameter({
            name: "Jingjin",
            attributeFilter: "SMID > -1"
        });
        getFeatureBySQLParams = new SuperMap.REST.GetFeaturesBySQLParameters({
            queryParameter: getFeatureParam,
            toIndex: 500,
            datasetNames: ["Jingjin:BaseMap_R"]
        });
        getFeatureBySQLService = new SuperMap.REST.GetFeaturesBySQLService(url2, {
            eventListeners: {"processCompleted": processCompleted, "processFailed": processFailed}
        });

        getFeatureBySQLService.processAsync(getFeatureBySQLParams);
    }

    function processCompleted(getFeaturesEventArgs) {
        var result = getFeaturesEventArgs.result;
        if (result && result.features) {
            themeLayer.addFeatures(result.features);
        }

        //显示图例
        document.getElementById("mapLegend").style.display = "block";
    }

    function processFailed(e) {
        widgets.alert.showAlert(e.error.errorMsg, false);
    }

    function clearLayer() {
        document.getElementById("mapLegend").style.display = "none";
        document.getElementById("infoBox").style.display = "none";
        //先清除上次的显示结果
        themeLayer.clear();
    }

    // 更新显示数据
    function updateData() {
        var feas = themeLayer.features;
        for (var i = 0, len = feas.length; i < len; i++) {
            var fea = feas[i];
            fea.attributes.POP_DENSITY99 = getRandomNumber(0, 0.2, 5);
        }
        themeLayer.redraw();
    }

    // 获取范围内的随机数
    // min - 范围下限
    // max - 范围上限
    // decimalNum - 返回结果的小数位数。如果为 0，返回整数。
    function getRandomNumber(min, max, decimalNum) {
        var rNum = min + Math.random() * (max - min);

        if (decimalNum) {
            if (!isNaN(decimalNum)) {
                return rNum;
            }
            else {
                decimalNum = parseInt(decimalNum);
            }

            if (decimalNum === 0) {
                return Math.round(rNum);
            }
            else {
                return parseFloat(rNum).toFixed(decimalNum);
            }
        }
        else {
            return rNum;
        }
    }

    //事件处理，控制信息框数据显示
    function evn(e) {
        if (e.target && e.target.refDataID) {
            document.getElementById("infoBox").style.display = "block";
            var fid = e.target.refDataID;
            var fea = themeLayer.getFeatureById(fid);
            if (fea) {
                document.getElementById("infoContent").innerHTML = "";
                document.getElementById("infoContent").innerHTML += "ID: " + fea.attributes.SMID + "<br/>";
                document.getElementById("infoContent").innerHTML += resources.text_districtName + fea.attributes.NAME + "<br/>";
                document.getElementById("infoContent").innerHTML += resources.text_populationDensity + ": " + parseFloat(fea.attributes.POP_DENSITY99).toFixed(5) + "<br/>";
            }
        }
        else {
            document.getElementById("infoContent").innerHTML = "";
            document.getElementById("infoBox").style.display = "none";
        }
    }
</script>
</body>
</html>